<template>
  <div class="a">
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <van-loading type="spinner" color="#1989fa" size="0.64rem" vertical style='padding-top:1.22667rem' v-if="!articleObj.title">
    努力加载中.....
    </van-loading>
    <div v-else>
      <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{articleObj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="articleObj.aut_name" :label="toDay(articleObj.pubdate)">
        <template #icon>
          <img :src="articleObj.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="articleObj.is_followed" @click="followedFn(false)">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="followedFn(true)">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="articleObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box" id="like" >
        <van-button icon="good-job" type="danger" size="small" v-if="articleObj.attitude===1" @click="likeFn(false)">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="likeFn(true)">点赞</van-button>
      </div>
    </div>
    <!-- 评论区域 -->
    <commentList :articleObj='articleObj'></commentList>
    </div>
  </div>
</template>

<script>
import CommentList from './CommentList.vue'
import { Notify } from 'vant'

import {
  articleDetailAPI,
  followedAuthorAPI,
  unfollowedAuthorAPI,
  articleDisLikingAPI,
  articleLikingAPI
} from '@/api'
import { toDay } from '../../directives/date' // 导入时间处理函数
export default {
  name: 'ArticleDetail',
  components: {
    CommentList
  },
  data () {
    return {
      articleObj: {}
    }
  },
  async  created () {
    try {
      const res = await articleDetailAPI({ id: this.$route.params.id })
      this.articleObj = res.data.data
    } catch (err) {
      // 如果找不到文章,提示并且跳转页面
      Notify({ type: 'warning', message: '暂时找不到此篇文章' })
      setTimeout(() => {
        this.$router.back()
      }, 1000)
    }
    // console.log(this.articleObj)
  },
  methods: {
    toDay, // 时间处理
    // 是否关注
    async followedFn (bool) {
      // 点击关注
      if (bool) {
        this.articleObj.is_followed = true
        await followedAuthorAPI({ aut_id: this.articleObj.aut_id })
      } else {
        // 取消关注
        this.articleObj.is_followed = false
        await unfollowedAuthorAPI({ id: this.articleObj.aut_id })
      }
    },
    // 是否喜欢
    async likeFn (bool) {
      if (bool) {
        // 点击喜欢-------变成红心
        this.articleObj.attitude = 1
        await articleLikingAPI({ art_id: this.articleObj.art_id })
      } else {
        // 点击不喜欢--------变成灰心
        this.articleObj.attitude = -1
        await articleDisLikingAPI({ id: this.articleObj.art_id })
      }
    }
  }
}
</script>

<style scoped lang="less">
.a{
  padding-bottom: 1.22667rem;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
    height: 100%;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
